﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using Bling.DSL;
using Bling.WPF;
using Bling.WPF.Util;
using Bling.Core;
using Bling.Util;
using Bling.Physics.UI;
using Bling.Physics;
using Bling.Properties;
using Bling.Mixins;
using Bling.Blocks;
using Bling.Shapes;
using Bling.Angles;

namespace Bling.Tutorial {
  public class PhysicsChain : PhysicsCanvas {
    public class RectBlock : TopBlock<RectBlock, PathShapeBl>, HasShape.IHas<RectBlock>, MouseInput.IHas<RectBlock, PathShapeBl> {
      public RectBlock(BlockManager Manager, CanvasBl Canvas, int Count)
        : base(Manager, Count, (block, idx) => {
          return new PathShapeBl(Canvas) {
            Fill = idx.SelectColor(),
          };
        }) { }
    }

    public class ThumbBlock : TopBlock<ThumbBlock, ThumbBl>, HasPosition.IHas<ThumbBlock> {
      public ThumbBlock(BlockManager Manager, CanvasBl Canvas, int Count)
        : base(Manager, Count, (block, idx) => {
          return new ThumbBl(Canvas) {
            Background = idx.SelectColor(),
            CanDrag = true,
            Size = new Double2Bl(10, 10),
          };
        }) { }
    }
    protected override Action Init(CanvasBl MainCanvas, BlockManager Manager) { // executes before blocks are initialized.
      MainCanvas.Background = Brushes.White;
        var Block = new ThumbBlock(Manager, MainCanvas, 100);
        var r = new Random();
        Block.ForAll = Elem => {
          Elem.Position().Init = Double2Bl.RandomFrom(r) * 500;
          Elem.Position().InstallVerletStep(.01);
          Elem.Position().Link[!Elem.At.IsDragging] = Elem.At.CenterPosition;
          Elem.Position().Relax[.1d] = Elem.Position().Clamp(new Double2Bl(0, 0), MainCanvas.Size);

          Elem.Position().Distance(Elem.Prev[1].Position()).Relax.To = /*[Elem.HasPrev[1]] =*/ 10d;
        };
      return () => { };
    }

  }
}